<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class PingjiaController extends Controller
{
	//列表页
    public function index(Request $request)
    {
    	$title = '商品评价列表';
    	//查询数据
        $data = DB::table('pingjia') -> where(function($query) use ($request)
        {
                           $query -> where('shopname','like','%'.$request 
                                  -> input('keywords').'%');
        }) 
                                  -> paginate($request -> input('num',10));
		// dd($data);
        return view('admin.pingjia.index',['title' => $title,'data' => $data,'request' => $request -> all()]);
    }

    //编辑页面
    public function edit($id)
    {
    	$title = '商品评价编辑';
        $data = DB::table('pingjia')
            -> where('id','=',$id) 
            -> first();
        //解析模板
        return view('admin.pingjia.edit',['title' => $title,'data' => $data]);

    }

     //执行更新页面
    public function update(Request $request)
    {
    	$data = $request -> except('_token','id');
    	// dd($data);
    	$id = $request -> input('id');
    	// dd($id);

    	//设置限制条件
    	$this -> validate($request, [
    			          'content' => 'required',
    		],[
    			 'content.required' => '评价内容不能为空',

    		]);
        //执行添加数据
        $res = DB::table('pingjia') 
            -> where('id',$id) 
            -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/pingjia/index') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }
    }

     //执行删除
    public function delete($id)
    {
    	$res = DB::table('pingjia') 
            -> where('id',$id) 
            -> delete();
    	if($res)
        {
            return redirect('/admin/pingjia/index') -> with(['success' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }
}
